---
title: "SALTY_Discordant"
author: "James"
date: '2023-04-22'
output: html_document
---

```{r Packages, echo = FALSE} 
require(OpenMx)
library(stringr)
require(umx)
require(haven)
require(broom)
require(tidyverse)
require(stargazer)
require(eeptools)
require(ggplot2)
require(eeptools)
set.seed(1234)
```

```{r Pre-Processing, echo = FALSE}

STR <- data.frame(read_dta("1 - Data/cohort_combined.dta"))
Salty = data.frame(read_dta("1 - Data/slty_map.dta"))


SALTY_combined = merge(Salty, STR, by.x = "LopNr", by.y = "LopNr")

SALTY_combined[SALTY_combined == 9] <- NA
SALTY_combined[SALTY_combined == 99] <- NA
SALTY_combined[SALTY_combined == 999] <- NA

#Trust

SALTY_combined$trust = ifelse(SALTY_combined$ATTITYD31 == 9, NA, SALTY_combined$ATTITYD31-1)
SALTY_combined$trust = 3-SALTY_combined$trust


SALTY_combined$newtrust = (3-SALTY_combined$trust)/3

SALTY_combined$trust_scaled = scale(SALTY_combined$trust)

#Next step - recoding and implementing turnout variables in analysis. 

#Behavioral

SALTY_combined <- SALTY_combined %>% mutate(
                          contact = 
                            ifelse(is.na(ATTITYD38_1) == F | is.na(ATTITYD38_2 == F) == F, yes = 1, no = 0),
                          contactpol = 
                          ifelse(is.na(ATTITYD38_1) == F, yes = 1, no = 0), 
                          contactpub = 
                          ifelse(is.na(ATTITYD38_2) == F, yes = 1, no = 0), 
                          protest = 
                          ifelse(is.na(ATTITYD38_3) == F, yes = 1, no = 0), 
                          boycott = 
                          ifelse(is.na(ATTITYD38_4) == F, yes = 1, no = 0), 
                          finance = 
                          ifelse(is.na(ATTITYD38_5) == F, yes = 1, no = 0), 
                          petition = 
                          ifelse(is.na(ATTITYD38_6) == F, yes = 1, no = 0), 
                          sum = (contactpol + contactpub + protest + finance + petition + boycott), 
                          newsum = ifelse(sum<=2, sum, 2), 
                          behavioral = scale(newsum))
                          
#Creating twin variables

SALTY_combined$FAMID <- as.factor(SALTY_combined$LopNrParID)
SALTY_combined$Byear = str_sub(SALTY_combined$Byea, end = - 3)
SALTY_combined$Byear = as.factor(SALTY_combined$Byear) 

#SALTY_combined$BDATE <- as.Date(SALTY_combined$Byear)
#SALTY_combined$AGE <- floor(age_calc(SALTY_combined$Byear, units = "years", precise = TRUE))

SALTY_combined_MZ <- subset(SALTY_combined, BESTZYG == 1)
SALTY_combined_DZ <- subset(SALTY_combined, BESTZYG != 1)

```

```{r Creating turnout analysis dataset}
Turnout_1970 = data.frame(read_dta("1 - Data/Lev_valdelt_1970.dta"))
Turnout_1994f = data.frame(read_dta("1 - Data/Lev_valdelt_1994f.dta"))
Turnout_1994rkl = data.frame(read_dta("1 - Data/Lev_valdelt_1994rkl.dta"))
Turnout_2010 = data.frame(read_dta("1 - Data/vd10.dta"))
Turnout_2009 = data.frame(read_dta("1 - Data/vd09.dta"))
Turnout_2018 = data.frame(read_dta("1 - Data/Lev_vd18.dta"))
Turnout_2019_EU = data.frame(read_dta("1 - Data/EU2019.dta"))

#Turnout

Turnout_1994 = merge(Turnout_1994f, Turnout_1994rkl, by.x = "LopNr", by.y = "LopNr", all = TRUE)
Turnout_20th = merge(Turnout_1994, Turnout_1970, by.x = "LopNr", by.y = "LopNr", all = TRUE) %>% 
  rename("Ref_1994" = "f", 
         "Ref_1994_Abroad" = "utlandsrost.x",
         "Abroad_1994" = "utlandsrost.y",
         "Nat_1994" = "r.x", 
         "Reg_1994" = "l.x", 
         "Mun_1994" = "k.x", 
         "Nat_1970" = "r.y", 
         "Reg_1970" = "l.y", 
         "Mun_1970" = "k.y", 
         "Legal_1970" = "omyndig", 
         "Foreign_1970" = "utlmedb")

Turnout_0s = merge(Turnout_2009, Turnout_2010, by.x = "LopNr", by.y = "LopNr", all= TRUE) %>% 
  rename("EU_2009" = "e",  
         "Nat_2010" = "r", 
         "Reg_2010" = "l", 
         "Mun_2010" = "k", 
         "Eligible_2010" = "Rostratt.x")

Turnout_10s = merge(Turnout_2018, Turnout_2019_EU, by.x = "LopNr", by.y = "LopNr", all = TRUE) %>% 
  rename("EU_2019" = "Eurost", 
         "Nat_2018" = "rrost", 
         "Reg_2018" = "lrost", 
         "Mun_2018" = "krost", 
         "Eligible_2018" = "Rostratt.x")

Turnout_most = merge(Turnout_20th, Turnout_0s, by.x = "LopNr", by.y. = "LopNr", all = TRUE)

Turnout_all = merge(Turnout_most, Turnout_10s, by.x = "LopNr", by.y = "LopNr", all = TRUE) %>% 
  select(LopNr, EU_2019, Nat_2018, Reg_2018, Mun_2018, Eligible_2018, Eligible_2010, Mun_2010, Reg_2010, Nat_2010, EU_2009, Mun_1970, Reg_1970, Nat_1970, Mun_1994, Reg_1994, Nat_1994, Ref_1994_Abroad, Ref_1994, Abroad_1994, 
         Legal_1970, Foreign_1970)

Turnout_analysis = merge(Turnout_all, SALTY_combined_MZ, by.x = "LopNr", by.y = "LopNr", all.y = TRUE) 
Turnout_analysis_DZ = merge(Turnout_all, SALTY_combined_DZ, by.x = "LopNr", by.y = "LopNr", all.y = TRUE) 

Turnout_analysis = Turnout_analysis %>% 
mutate(EU_2009 = dplyr::recode(EU_2009, "1" = 0, 
                                 "2" = 1, 
                                 "3" = 2, 
                                 "4" = 1, 
                                 "5" = 1, 
                                 "6" = 1), 
         EU_2009 = na_if(EU_2009, 2), 
         Ref_1994 = dplyr::recode(Ref_1994, "1" = 0, 
                                 "2" = 1, 
                                 "3" = 2, 
                                 "4" = 1, 
                                 "5" = 1, 
                                 "6" = 1), 
         Ref_1994 = na_if(Ref_1994, 2),
         Nat_1970 = case_when(Nat_1970 == "1" ~ 0, 
                              Nat_1970 == "2" ~ 1, 
                              Nat_1970 == "3" ~ NA, 
                              Nat_1970 == "4" ~ 1, 
                              Nat_1970 == "5" ~ 1, 
                              Nat_1970 == "6" ~ 1, 
                              Legal_1970 == "1"|Foreign_1970 == "1" ~ NA), 
         Reg_1970 = case_when(Reg_1970 == "1" ~ 0, 
                              Reg_1970 == "2" ~ 1, 
                              Reg_1970 == "3" ~ NA, 
                              Reg_1970 == "4" ~ 1, 
                              Reg_1970 == "5" ~ 1, 
                              Reg_1970 == "6" ~ 1, 
                              Legal_1970 == "1"|Foreign_1970 == "1" ~ NA),
         Mun_1970 = case_when(Mun_1970 == "1" ~ 0, 
                              Mun_1970 == "2" ~ 1, 
                              Mun_1970 == "3" ~ NA, 
                              Mun_1970 == "4" ~ 1, 
                              Mun_1970 == "5" ~ 1, 
                              Mun_1970 == "6" ~ 1, 
                              Legal_1970 == "1"|Foreign_1970 == "1" ~ NA), 
         Nat_1994 = case_when(Nat_1994 == "1" ~ 0, 
                              Nat_1994 == "2" ~ 1, 
                              Nat_1994 == "3" ~ NA, 
                              Nat_1994 == "4" ~ 1, 
                              Nat_1994 == "5" ~ 1, 
                              Nat_1994 == "6" ~ 1), 
         Reg_1994 = case_when(Reg_1994 == "1" ~ 0, 
                              Reg_1994 == "2" ~ 1, 
                              Reg_1994 == "3" ~ NA, 
                              Reg_1994 == "4" ~ 1, 
                              Reg_1994 == "5" ~ 1, 
                              Reg_1994 == "6" ~ 1), 
         Mun_1994 = case_when(Mun_1994 == "1" ~ 0, 
                              Mun_1994 == "2" ~ 1, 
                              Mun_1994 == "3" ~ NA, 
                              Mun_1994 == "4" ~ 1, 
                              Mun_1994 == "5" ~ 1, 
                              Mun_1994 == "6" ~ 1), 
         Nat_2010 = case_when(Nat_2010 == "1" ~ 0, 
                              Nat_2010 == "2" ~ 1, 
                              Nat_2010 == "3" ~ NA, 
                              Nat_2010 == "4" ~ 1, 
                              Nat_2010 == "5" ~ 1, 
                              Nat_2010 == "6" ~ 1, 
                              Eligible_2010 == "3" ~ NA), 
         Reg_2010 = case_when(Reg_2010 == "1" ~ 0, 
                              Reg_2010 == "2" ~ 1, 
                              Reg_2010 == "3" ~ NA, 
                              Reg_2010 == "4" ~ 1, 
                              Reg_2010 == "5" ~ 1, 
                              Reg_2010 == "6" ~ 1, 
                              Eligible_2010 == "2" ~ NA), 
         Mun_2010 = case_when(Mun_2010 == "1" ~ 0, 
                              Mun_2010 == "2" ~ 1, 
                              Mun_2010 == "3" ~ NA, 
                              Mun_2010 == "4" ~ 1, 
                              Mun_2010 == "5" ~ 1, 
                              Mun_2010 == "6" ~ 1, 
                              Eligible_2010 == "2" ~ NA), 
         Nat_2018 = case_when(Nat_2018 == "1" ~ 1, 
                              Nat_2018 == "0" ~ 0, 
                              
                              Eligible_2018 == "3" ~ NA), 
         Reg_2018 = case_when(Reg_2018 == "1" ~ 1, 
                              Reg_2018 == "0" ~ 0, 
                              
                              Eligible_2018 == "2" ~ NA), 
         Mun_2018 = case_when(Mun_2018 == "1" ~ 1, 
                              Mun_2018 == "0" ~ 0, 
                              
                              Eligible_2018 == "2" ~ NA))

Turnout_analysis = Turnout_analysis %>% 
  mutate(National_total = rowSums(Turnout_analysis[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", "Ref_1994")], na.rm = TRUE), 
         National_eligible = rowSums(!is.na(Turnout_analysis[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", "Ref_1994") ])),
         Regional_total = rowSums(Turnout_analysis[,c("Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970") ], na.rm = TRUE), 
         Regional_eligible = rowSums(!is.na(Turnout_analysis[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970") ])), 
         Municipal_total = rowSums(Turnout_analysis[,c("Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970") ], na.rm = TRUE), 
         Municipal_eligible = rowSums(!is.na(Turnout_analysis[,c("Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970") ])), 
         EU_total = rowSums(Turnout_analysis[,c("EU_2019", "EU_2009")], na.rm = TRUE), 
         EU_eligible = rowSums(!is.na(Turnout_analysis[,c("EU_2019", "EU_2009") ])), 
         Total_total = rowSums(Turnout_analysis[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                   "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                   "Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970", 
                                                   "EU_2019", "EU_2009")], na.rm = TRUE), 
         Total_eligible = rowSums(!is.na(Turnout_analysis[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                                "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                                "Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970", 
                                                                "EU_2019", "EU_2009")])), 
         Domestic_total = rowSums(Turnout_analysis[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                   "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                   "Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970", "Ref_1994")], na.rm = TRUE), 
         Domestic_eligible = rowSums(!is.na(Turnout_analysis[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                                "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                                "Mun_2018", "Mun_2010", "Mun_1994", "Ref_1994", "Mun_1970")])),
                                       National_prop = National_total/National_eligible, 
                                       Regional_prop = Regional_total/Regional_eligible, 
                                       Municipal_prop = Municipal_total/Municipal_eligible, 
                                       EU_prop = EU_total/EU_eligible, 
         Total_prop = Total_total/Total_eligible, 
                  National_test = scale(ifelse(National_prop == 1, National_prop, 0)),

         Domestic_prop = Domestic_total/Domestic_eligible, 
         Domestic_scaled = scale(Domestic_prop), 
         Total_scaled = scale(Total_prop), 
         Regional_scaled = scale(Regional_prop), 
         Municipal_scaled = scale(Municipal_prop), 
         National_scaled = scale(National_prop), 
         EU_scaled = scale(EU_prop))

Turnout_analysis = Turnout_analysis %>% 
mutate(EU_2009 = dplyr::recode(EU_2009, "1" = 0, 
                                 "2" = 1, 
                                 "3" = 2, 
                                 "4" = 1, 
                                 "5" = 1, 
                                 "6" = 1), 
         EU_2009 = na_if(EU_2009, 2), 
         Ref_1994 = dplyr::recode(Ref_1994, "1" = 0, 
                                 "2" = 1, 
                                 "3" = 2, 
                                 "4" = 1, 
                                 "5" = 1, 
                                 "6" = 1), 
         Ref_1994 = na_if(Ref_1994, 2),
         Nat_1970 = case_when(Nat_1970 == "1" ~ 0, 
                              Nat_1970 == "2" ~ 1, 
                              Nat_1970 == "3" ~ NA, 
                              Nat_1970 == "4" ~ 1, 
                              Nat_1970 == "5" ~ 1, 
                              Nat_1970 == "6" ~ 1, 
                              Legal_1970 == "1"|Foreign_1970 == "1" ~ NA), 
         Reg_1970 = case_when(Reg_1970 == "1" ~ 0, 
                              Reg_1970 == "2" ~ 1, 
                              Reg_1970 == "3" ~ NA, 
                              Reg_1970 == "4" ~ 1, 
                              Reg_1970 == "5" ~ 1, 
                              Reg_1970 == "6" ~ 1, 
                              Legal_1970 == "1"|Foreign_1970 == "1" ~ NA),
         Mun_1970 = case_when(Mun_1970 == "1" ~ 0, 
                              Mun_1970 == "2" ~ 1, 
                              Mun_1970 == "3" ~ NA, 
                              Mun_1970 == "4" ~ 1, 
                              Mun_1970 == "5" ~ 1, 
                              Mun_1970 == "6" ~ 1, 
                              Legal_1970 == "1"|Foreign_1970 == "1" ~ NA), 
         Nat_1994 = case_when(Nat_1994 == "1" ~ 0, 
                              Nat_1994 == "2" ~ 1, 
                              Nat_1994 == "3" ~ NA, 
                              Nat_1994 == "4" ~ 1, 
                              Nat_1994 == "5" ~ 1, 
                              Nat_1994 == "6" ~ 1), 
         Reg_1994 = case_when(Reg_1994 == "1" ~ 0, 
                              Reg_1994 == "2" ~ 1, 
                              Reg_1994 == "3" ~ NA, 
                              Reg_1994 == "4" ~ 1, 
                              Reg_1994 == "5" ~ 1, 
                              Reg_1994 == "6" ~ 1), 
         Mun_1994 = case_when(Mun_1994 == "1" ~ 0, 
                              Mun_1994 == "2" ~ 1, 
                              Mun_1994 == "3" ~ NA, 
                              Mun_1994 == "4" ~ 1, 
                              Mun_1994 == "5" ~ 1, 
                              Mun_1994 == "6" ~ 1), 
         Nat_2010 = case_when(Nat_2010 == "1" ~ 0, 
                              Nat_2010 == "2" ~ 1, 
                              Nat_2010 == "3" ~ NA, 
                              Nat_2010 == "4" ~ 1, 
                              Nat_2010 == "5" ~ 1, 
                              Nat_2010 == "6" ~ 1, 
                              Eligible_2010 == "3" ~ NA), 
         Reg_2010 = case_when(Reg_2010 == "1" ~ 0, 
                              Reg_2010 == "2" ~ 1, 
                              Reg_2010 == "3" ~ NA, 
                              Reg_2010 == "4" ~ 1, 
                              Reg_2010 == "5" ~ 1, 
                              Reg_2010 == "6" ~ 1, 
                              Eligible_2010 == "2" ~ NA), 
         Mun_2010 = case_when(Mun_2010 == "1" ~ 0, 
                              Mun_2010 == "2" ~ 1, 
                              Mun_2010 == "3" ~ NA, 
                              Mun_2010 == "4" ~ 1, 
                              Mun_2010 == "5" ~ 1, 
                              Mun_2010 == "6" ~ 1, 
                              Eligible_2010 == "2" ~ NA), 
         Nat_2018 = case_when(Nat_2018 == "1" ~ 1, 
                              Nat_2018 == "0" ~ 0, 
                              
                              Eligible_2018 == "3" ~ NA), 
         Reg_2018 = case_when(Reg_2018 == "1" ~ 1, 
                              Reg_2018 == "0" ~ 0, 
                              
                              Eligible_2018 == "2" ~ NA), 
         Mun_2018 = case_when(Mun_2018 == "1" ~ 1, 
                              Mun_2018 == "0" ~ 0, 
                              
                              Eligible_2018 == "2" ~ NA)) 

Turnout_analysis_DZ$Nat_1970 = case_when(Turnout_analysis_DZ$Nat_1970==1 ~ 0, 
                                         Turnout_analysis_DZ$Nat_1970==2 ~ 1, 
                                         Turnout_analysis_DZ$Nat_1970>2 ~ 1, 
                                         .default = NA)
Turnout_analysis_DZ$Nat_1994 = case_when(Turnout_analysis_DZ$Nat_1994==1 ~ 0, 
                                         Turnout_analysis_DZ$Nat_1994==2 ~ 1, 
                                         Turnout_analysis_DZ$Nat_1994>2 ~ 1, 
                                         .default = NA)
Turnout_analysis_DZ$Nat_2010 = case_when(Turnout_analysis_DZ$Nat_2010==1 ~ 0, 
                                         Turnout_analysis_DZ$Nat_2010==2 ~ 1, 
                                         Turnout_analysis_DZ$Nat_2010>2 ~ 1, 
                                         .default = NA)


Turnout_analysis_DZ = Turnout_analysis_DZ %>% 
  mutate(National_total = rowSums(Turnout_analysis_DZ[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970")], na.rm = TRUE), 
         National_eligible = rowSums(!is.na(Turnout_analysis_DZ[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970") ])),
         Regional_total = rowSums(Turnout_analysis_DZ[,c("Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970") ], na.rm = TRUE), 
         Regional_eligible = rowSums(!is.na(Turnout_analysis_DZ[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970") ])), 
         Municipal_total = rowSums(Turnout_analysis_DZ[,c("Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970") ], na.rm = TRUE), 
         Municipal_eligible = rowSums(!is.na(Turnout_analysis_DZ[,c("Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970") ])), 
         EU_total = rowSums(Turnout_analysis_DZ[,c("EU_2019", "EU_2009")], na.rm = TRUE), 
         EU_eligible = rowSums(!is.na(Turnout_analysis_DZ[,c("EU_2019", "EU_2009") ])), 
         Total_total = rowSums(Turnout_analysis_DZ[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                   "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                   "Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970", 
                                                   "EU_2019", "EU_2009")], na.rm = TRUE), 
         Total_eligible = rowSums(!is.na(Turnout_analysis_DZ[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                                "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                                "Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970", 
                                                                "EU_2019", "EU_2009")])), 
         Domestic_total = rowSums(Turnout_analysis_DZ[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                   "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                   "Mun_2018", "Mun_2010", "Mun_1994", "Mun_1970", "Ref_1994")], na.rm = TRUE), 
         Domestic_eligible = rowSums(!is.na(Turnout_analysis_DZ[,c("Nat_2018", "Nat_2010", "Nat_1994", "Nat_1970", 
                                                                "Reg_2018", "Reg_2010", "Reg_1994", "Reg_1970", 
                                                                "Mun_2018", "Mun_2010", "Mun_1994", "Ref_1994", "Mun_1970")])),
                                       National_prop = National_total/National_eligible, 
         National_test = scale(ifelse(National_prop == 1, National_prop, 0)),
                                       Regional_prop = Regional_total/Regional_eligible, 
                                       Municipal_prop = Municipal_total/Municipal_eligible, 
                                       EU_prop = EU_total/EU_eligible, 
         Total_prop = Total_total/Total_eligible, 
         Domestic_prop = Domestic_total/Domestic_eligible, 
         Domestic_scaled = scale(Domestic_prop), 
         Total_scaled = scale(Total_prop), 
         Regional_scaled = scale(Regional_prop), 
         Municipal_scaled = scale(Municipal_prop), 
         National_scaled = scale(National_prop), 
         EU_scaled = scale(EU_prop))
```

```{r Descriptive statistics}

Turnout_analysis[, 254:293] %>% 
 filter(BESTZYG == 1) %>% 
  select(SEX,trust, newtrust, contactpol, contactpub, protest, boycott, finance, petition, newsum, National_prop) %>% 
 stargazer(iqr = T, median = T, out = "descriptive_stats_MZ.htm")

Turnout_analysis_DZ[, 254:293] %>% 
  filter(BESTZYG != 1) %>% 
    select(SEX, trust, newtrust, contactpol, contactpub, protest, boycott, finance, petition, newsum, National_prop) %>% 
  stargazer(iqr = T, median = T, out = "descriptive_stats_DZ.htm")

```

```{r Phenotype Correlations}

Turnout_analysis_DZ %>% 
  select("trust_scaled", "behavioral",  
         "National_scaled", "Total_prop") %>% 
  psych::corr.test(ci = T) %>% 
  print(short = F)

```

```{r Models, Main (Appendix Tables A3g, A3h)}

m1 <- lm(behavioral ~ trust_scaled + SEX + Byear + SEX*Byear, data = SALTY_combined_MZ)

m1FE <- lm(behavioral ~ trust_scaled + FAMID, data = SALTY_combined_MZ)

m2 <- lm(National_scaled ~ trust_scaled + SEX + Byear + SEX*Byear, data = Turnout_analysis)

m2FE <- lm(National_scaled ~ trust_scaled + FAMID, data = Turnout_analysis)

m3 <- lm(behavioral ~ trust_scaled + SEX + Byear + SEX*Byear, data = SALTY_combined_DZ)

m3FE <- lm(behavioral ~ trust_scaled + FAMID, data = SALTY_combined_DZ)

m4 <- lm(National_scaled ~ trust_scaled + SEX + Byear + SEX*Byear, data = Turnout_analysis_DZ)

m4FE <- lm(National_scaled ~ trust_scaled + FAMID, data = Turnout_analysis_DZ)


```

```{r Tables, Main (Appendix Tables A3g, A3h)}

stargazer(m1, m1FE, m2, m2FE, type = "text", omit = c("FAMID", "Byear"),
          dep.var.labels = c("Behavioral scale", "National election turnout"), 
          out = "2 - Tables/Discordant_headline.htm")

stargazer(m3, m3FE, m4, m4FE, type = "text", omit = c("FAMID", "Byear"),
          dep.var.labels = c("Behavioral scale", "National election turnout"),  
          out = "2- Tables/Discordant_headline_DZ.htm")


```

```{r Paternoster tests}

#Behavioral scale

Z1 = (0.140-0.069)/sqrt(I(0.018)^2+I(0.031)^2)
Z1

#Turnout scale

Z2 = (0.171-0.073)/sqrt(I(0.019)^2+I(0.032)^2)
Z2

```

```{r Models, Behavioral (Appendix Table A3y)}

###1 - Contactpol

b1 <- lm(contactpol ~ trust + SEX + Byear + SEX*Byear, data = SALTY_combined_MZ)

b1FE <- lm(contactpol ~ trust + FAMID, data = SALTY_combined_MZ)

###2 - Contactpub

b2 <- lm(contactpub ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_MZ)

b2FE <- lm(contactpub ~ trust + FAMID, data = SALTY_combined_MZ)

###3 - Protest

b3 <- lm(protest ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_MZ)

b3FE <- lm(protest ~ trust + FAMID, data = SALTY_combined_MZ)

###4 - Boycott

b4 <- lm(boycott ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_MZ)

b4FE <- lm(boycott ~ trust + FAMID, data = SALTY_combined_MZ)

###5 - Finance

b5 <- lm(finance ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_MZ)

b5FE <- lm(finance ~ trust  +FAMID, data = SALTY_combined_MZ)

###6 - Petition

b6 <- lm(petition ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_MZ)

b6FE <- lm(petition ~ trust  + FAMID, data = SALTY_combined_MZ)

```

```{r Tables, Behavioral (Appendix Table A3y)}

stargazer(b1, b1FE, b2, b2FE, b3, b3FE, b4, b4FE, b5, b5FE, b6, b6FE, type = "text",  
          dep.var.labels = c("Contact politician", "Contact public official", "Attend a protest", "Boycott", "Donation", "Petition"), omit = c("Byear", "FAMID"), 
          covariate.labels = c("Political trust", "Sex"),
          out = "Discordant_Behav_Individual.htm")

```

```{r Models, Behavioral, DZ (Appendix Table A3z)}

###1 - Contactpol

b1 <- lm(contactpol ~ trust + SEX + Byear + SEX*Byear, data = SALTY_combined_DZ)

b1FE <- lm(contactpol ~ trust + FAMID, data = SALTY_combined_DZ)

###2 - Contactpub

b2 <- lm(contactpub ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_DZ)

b2FE <- lm(contactpub ~ trust + FAMID, data = SALTY_combined_DZ)

###3 - Protest

b3 <- lm(protest ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_DZ)

b3FE <- lm(protest ~ trust + FAMID, data = SALTY_combined_DZ)

###4 - Boycott

b4 <- lm(boycott ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_DZ)

b4FE <- lm(boycott ~ trust + FAMID, data = SALTY_combined_DZ)

###5 - Finance

b5 <- lm(finance ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_DZ)

b5FE <- lm(finance ~ trust  +FAMID, data = SALTY_combined_DZ)

###6 - Petition

b6 <- lm(petition ~ trust + SEX + Byear + SEX*Byear , data = SALTY_combined_DZ)

b6FE <- lm(petition ~ trust  + FAMID, data = SALTY_combined_DZ)

```
 
```{r Tables, Behavioral, DZ (Appendix Table A3z)}

stargazer(b1, b1FE, b2, b2FE, b3, b3FE, b4, b4FE, b5, b5FE, b6, b6FE, type = "text", 
          dep.var.labels = c("Contact politician", "Contact public official", "Attend a protest", "Boycott", "Donation", "Petition"), omit = c("Byear", "FAMID"), 
          covariate.labels = c("Political trust", "Sex"),
          out = "Discordant_Behav_Individual_DZ.htm")

```

```{r Models, Turnout (Appendix Table A3aa)}

###1 - Overall

t1 <- lm(Domestic_scaled ~ trust_scaled + SEX + Byear + SEX*Byear, data = Turnout_analysis)

t1FE <- lm(Domestic_scaled ~ trust_scaled + FAMID, data = Turnout_analysis)

###2 - National

t2 <- lm(National_scaled ~ trust_scaled + SEX + Byear + SEX*Byear , data = Turnout_analysis)

t2FE <- lm(National_scaled ~ trust_scaled + FAMID, data = Turnout_analysis)

###3 - Regional

t3 <- lm(Regional_scaled ~ trust_scaled + SEX + Byear + SEX*Byear , data = Turnout_analysis)

t3FE <- lm(Regional_scaled ~ trust_scaled + FAMID, data = Turnout_analysis)

###4 - Municipal

t4 <- lm(Municipal_scaled ~ trust_scaled + SEX + Byear + SEX*Byear , data = Turnout_analysis)

t4FE <- lm(Municipal_scaled ~ trust_scaled + FAMID, data = Turnout_analysis)


```

```{r Tables, Turnout (Appendix Table A3aa)}

stargazer(t1, t1FE, t2, t2FE, t3, t3FE, t4, t4FE, omit = c("FAMID", "Byear"),  type = "text",
          covariate.labels = c("Political trust", "Sex"),
          out = "Discordant_Turnout.htm")
```

```{r Models, Turnout, DZ (Appendix Table A3ab)}

###1 - Overall

t1 <- lm(Domestic_scaled ~ trust_scaled + SEX + Byear + SEX*Byear, data = Turnout_analysis_DZ)

t1FE <- lm(Domestic_scaled ~ trust_scaled + FAMID, data = Turnout_analysis_DZ)

###2 - National

t2 <- lm(National_scaled ~ trust_scaled + SEX + Byear + SEX*Byear , data = Turnout_analysis_DZ)

t2FE <- lm(National_scaled ~ trust_scaled + FAMID, data = Turnout_analysis_DZ)

###3 - Regional     

t3 <- lm(Regional_scaled ~ trust_scaled + SEX + Byear + SEX*Byear , data = Turnout_analysis_DZ)

t3FE <- lm(Regional_scaled ~ trust_scaled + FAMID, data = Turnout_analysis_DZ)

###4 - Municipal

t4 <- lm(Municipal_scaled ~ trust_scaled + SEX + Byear + SEX*Byear , data = Turnout_analysis_DZ)

t4FE <- lm(Municipal_scaled ~ trust_scaled + FAMID, data = Turnout_analysis_DZ)



```

```{r Tables, Turnout, DZ (Appendix Table A3ab)}

stargazer(t1, t1FE, t2, t2FE, t3, t3FE, t4, t4FE, omit = c("FAMID", "Byear"), 
          #add.lines = list(power_turnout_items), 
          covariate.labels = c("Political trust", "Sex"),
          out = "Discordant_Turnout_DZ.htm")

```



